--
-- Author: yawei.zhu
-- Date: 2018-08-03 17:45:42
--

local RoadHeroProcess = class("RoadHeroProcess")

function RoadHeroProcess:ctor(msg)
	if msg then
		self:read(msg)
	end
end

function RoadHeroProcess:read(ret)
	self.level = ret.Level
	self.step = ret.Step
	self.boxStatus = {}
	for i, data in ipairs(ret.BoxStatus) do
		self.boxStatus[i] = data
	end
end

-- 获取下一个可打的节点
function RoadHeroProcess:getNextPoint()
	local nextLevel = self.level
	local nextStep = self.step + 1
	if GD:getHeroInfoByIndex(nextLevel, nextStep) then 
		return nextLevel, nextStep
	end

	nextLevel = self.level + 1
	nextStep = 1
	if GD:getHeroInfoByIndex(nextLevel, nextStep) then 
		return nextLevel, nextStep
	end

	-- 全部通关
	return 0, 0
end

-- 判断当前是否有宝箱可打开
function RoadHeroProcess:checkHasAward()
	for i = 1, self.level do
		if not self:checkIsAward(i) and self:checkHeroLevelIsPass(i) then
			return true
		end
	end
	return false
end

-- 判断英雄之路 当前层级 是否全部通关
function RoadHeroProcess:checkHeroLevelIsPass(level)

	if level < self.level then return true end
	if level > self.level then return false end

	if GD:getHeroInfoByIndex(self.level, self.step + 1) == nil then return true end
	return false 
end

-- 判断当前层级 是否已领奖
function RoadHeroProcess:checkIsAward(level)
	return self.boxStatus[level]
end

-- 判断当前节点是否已通关
function RoadHeroProcess:checkIsPass(level, step) 
	if level < self.level then return true end
	if level > self.level then return false end
	if step <= self.step then return true end
	return false 
end

-- 判断当前节点是否是下一个可打的节点
function RoadHeroProcess:checkIsNext(level, step)
	-- 没有配置就直接返回
	if not GD:getHeroInfoByIndex(level, step) then return false end

	local nextLevel, nextStep = self:getNextPoint()
	return nextLevel == level and nextStep == step
end

-- 判断当前节点是否可打（已通关 或者 下一个节点）
function RoadHeroProcess:checkCanChallenge(level, step)
	if self:checkIsPass(level, step) or self:checkIsNext(level, step) then return true end
	return false
end

return RoadHeroProcess
